package com.activity;

import muslim.toolkit.R;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.AdapterView.OnItemSelectedListener;

import com.PrayTime.PrayTimeLayout;
import com.calque.CalqueSites;
import com.calque.CalqueSitesHavePicture;
import com.commun.AjouterMosque;
import com.commun.Param;
import com.commun.Utils;
import com.data.DataBaseQuery;
import com.geo.GeoTools;
import com.geo.MyMapView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MyLocationOverlay;
import com.seekBar.RangeSeekBar;
import com.seekBar.RangeSeekBar.OnRangeSeekBarChangeListener;
import com.seekBar.RangeSeekBar.PRAY;
import com.service.LocalServiceTimer;
import com.service.SilentService;
import com.service.AlarmServiceInterface.Stub;
import com.service.LocalServiceTimer.AlarmServiceInterfaceImp;

public class MyApplicationMainActivity extends MapActivity {
	private static MyMapView myMapView;
	private MapController mapController;
	public final static Boolean DEVELOPER_MODE = true;
	private Boolean MapIntitialised = false;
	private MyLocationOverlay locationOverlay;
	private TabHost monTabHost;
	public static Location myLocation;
	public static GeoPoint myGeoPoint;
	public static Double mLatitude;
	public static Double mLongitude;
	public static Context mContext;
	private SharedPreferences app_preferences;
	public static boolean isNetworkConnected = false;
	public static boolean is_dialog_error_connection_fail_diplayed;

	public static Context getmContext() {
		return mContext;
	}
	public static  MyMapView getMyMapView() {
		return myMapView;
	}
	public static void setmContext(Context mContext) {
		MyApplicationMainActivity.mContext = mContext;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		if (DEVELOPER_MODE) {
			// StrictMode.enableDefaults(); //2.2
		}

		HideTitleAndIconOfApplication(true);

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		IndependenteInitialise();
		InitialisationLocation();
		IntitialisationAnglet();
		IntialisationButtons();
		InitialisationServices();
		// if(isNetworkConnected)
		// InitialisationMarqueurs();
		Log.d("Creat", "on");
	}

	private void HideTitleAndIconOfApplication(boolean bool) {
		if (bool)
			requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	private void IndependenteInitialise() {
		mContext = this;
		DataBaseQuery.firstResumeForMosquePosition = true;
		DataBaseQuery.rayonChanged = true;
		isNetworkConnected = false;
		CalqueSites.drawRoute = false;
		CalqueSitesHavePicture.drawRoute = false;
		Utils.TOAST_TEXT_LAUNCHED = false;
		app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
		initNetworkAvaileble();
		
	} 
	private void initNetworkAvaileble(){
		Handler h = new Handler() {
			@Override
			public void handleMessage(Message msg) {

				if (msg.what != 1) { // code if not connected
					isNetworkConnected = false;
				} else { // code if connected
					isNetworkConnected = true;
					InitialisationMarqueurs();
				}
			}
		};
		Utils.isNetworkAvailable(h, 2000);
	};

	private void InitialisationMarqueurs() {

		myMapView = Utils.MapMarqueurSetup(myMapView, this);

	}

	public TabHost getMonTabHost() {
		return monTabHost;
	}

	public void setMonTabHost(TabHost monTabHost) {
		this.monTabHost = monTabHost;
	}

	public void IntitialisationAnglet() {

		monTabHost = Utils.setupTabHost(
				(TabHost) findViewById(R.id.TabHostMenu), this);
	}

	private void InitialisationServices() {
		InitialisationSettingTabTimerNotification(this);
		InitialisationSettingTabSilentMode(this);
	}

	private void InitialisationLocation() {

		myMapView = (MyMapView) findViewById(R.id.mapView);
		mapController = myMapView.getController();
		mapController.setZoom(Param.ZOOM_LEVEL);
		myMapView.getZoomButtonsController();
		myMapView.setSatellite(false);

		locationOverlay = new MyLocationOverlay(MyApplicationMainActivity.this,
				myMapView);
		myMapView.getOverlays().add(locationOverlay);
		if (DEVELOPER_MODE) {
			mLatitude = 48.855279;// 36.859502;//
			mLongitude = 2.287939;// 10.168097;//
			myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
			mapController.animateTo(myGeoPoint);

		} else {
			InitialisationCurrentLocation();
		}

		MapIntitialised = true;

	}

	ImageButton zoomInBotton;
	ImageButton zoomOutBotton;
	ImageButton ajouterMosqueeSettingButton;
	ImageButton switchModeSatelliteMapsButton;
	ImageButton isRouteViewActiveButton;
	public static ImageButton ajouterMosqueeTagButton;
	public static Button terminerajouterMosqueeTagButton;
	Button autoSilentSettingsButton;
	Boolean modeSatellite = false;
	private void IntialisationButtons() {
		zoomInBotton = (ImageButton) findViewById(R.id.ZoomInButton);
		zoomOutBotton = (ImageButton) findViewById(R.id.ZoomOutButton);
		ajouterMosqueeSettingButton = (ImageButton) findViewById(R.id.boutton_ajout_setting_mosquee);
		ajouterMosqueeTagButton = (ImageButton) findViewById(R.id.ajout_mosque_tag_buttom);
		terminerajouterMosqueeTagButton = (Button) findViewById(R.id.terminer_ajout_mosque_tag_buttom);
		autoSilentSettingsButton = (Button) findViewById(R.id.boutton_congif_mise_en_mode_selencieux_heure_priere);
		switchModeSatelliteMapsButton =(ImageButton) findViewById(R.id.boutton_mode_satellite);
		isRouteViewActiveButton  =(ImageButton) findViewById(R.id.boutton_mode_route_view);
		zoomInBotton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mapController.zoomIn();
				verifZoomPictureOnZoom();
			}
		});
		zoomOutBotton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mapController.zoomOut();
				verifZoomPictureOnZoom();
			}
		});

		ajouterMosqueeSettingButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (isNetworkConnected)
					AjouterMosque.EditAjoutMosqueEnabling();
			}

		});
		ajouterMosqueeTagButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				if (!AjouterMosque.isTagButtonActivate) {
					ajouterMosqueeTagButton
							.setBackgroundResource(R.drawable.blue_btn_small_focused);
					AjouterMosque.isTagButtonActivate = true;
				} else {
					ajouterMosqueeTagButton
							.setBackgroundResource(R.drawable.blue_btn_small_normal);
					AjouterMosque.isTagButtonActivate = false;
				}

			} 

		});
		terminerajouterMosqueeTagButton
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						AjouterMosque
								.TeminerAjoutMosque(MyApplicationMainActivity
										.getmContext());

					}

				});
		switchModeSatelliteMapsButton
		.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				myMapView.setSatellite(modeSatellite= !modeSatellite);
				if (modeSatellite) {
					switchModeSatelliteMapsButton
							.setImageDrawable(getResources().getDrawable(R.drawable.maps_mode));
				} else {
					switchModeSatelliteMapsButton
							.setImageDrawable(getResources().getDrawable(R.drawable.satellite_mode));
				}
			}

		});
		isRouteViewActiveButton
		.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (!CalqueSites.isDrawingRouteActive) {
					isRouteViewActiveButton
							.setBackgroundResource(R.drawable.blue_btn_small_disabled);
					CalqueSites.isDrawingRouteActive = true;
				} else {
					isRouteViewActiveButton
							.setBackgroundResource(R.drawable.blue_btn_small_normal);
					CalqueSites.isDrawingRouteActive = false;
				}
			}

		});
		autoSilentSettingsButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				final ScrollView scroller = (ScrollView) findViewById(R.id.OngletSetting);
				final LinearLayout layout = (LinearLayout) findViewById(R.id.auto_silent_settings_layout_mother);
				if (!isSilentSettingPrayTimeDisplayed) {
					isSilentSettingPrayTimeDisplayed = true;
					autoSilentSettingsButton
							.setCompoundDrawablesWithIntrinsicBounds(
									R.drawable.mute_blue, R.drawable.nop,
									R.drawable.btn_dropdown_up, R.drawable.nop);
					layout.setLayoutParams(new LinearLayout.LayoutParams(
							android.view.ViewGroup.LayoutParams.FILL_PARENT,
							android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
				} else {
					isSilentSettingPrayTimeDisplayed = false;
					autoSilentSettingsButton
							.setCompoundDrawablesWithIntrinsicBounds(
									R.drawable.mute_blue, R.drawable.nop,
									R.drawable.btn_dropdown_down,
									R.drawable.nop);
					layout
							.setLayoutParams(new LinearLayout.LayoutParams(
									android.view.ViewGroup.LayoutParams.FILL_PARENT,
									0));
				}

				final Handler mHandler = new Handler();
				final Runnable mUpdateTimeTask = new Runnable() {
					public void run() {

						scroller.fullScroll(View.FOCUS_DOWN);
					}
				};
				mHandler.removeCallbacks(mUpdateTimeTask);
				mHandler.postDelayed(mUpdateTimeTask, 50);

			}
		});
	}

	private void verifZoomPictureOnZoom(){
		
		if(myMapView.getZoomLevel()==myMapView.getMaxZoomLevel())zoomInBotton.setImageResource(R.drawable.zoom_in_full);
		else zoomInBotton.setImageResource(R.drawable.zoom_in);
		if(myMapView.getZoomLevel()== 1)zoomOutBotton.setImageResource(R.drawable.zoom_out_full);
		else zoomOutBotton.setImageResource(R.drawable.zoom_out);
		//Log.w("zoom",myMapView.getZoomLevel()+"");
	}
	private boolean isSilentSettingPrayTimeDisplayed = false;

	public int inc(int i) {
		return i++;
	}

	@Override
	public void onResume() {
		super.onResume();
		
		if (MapIntitialised)
			locationOverlay.enableCompass();
			
		Log.d("Resume", "on");
	}

	@Override
	public void onPause() {
		super.onPause();
		if (MapIntitialised)
			locationOverlay.disableCompass();
		Log.d("Pause", "on");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		MyApplicationMainActivity.is_dialog_error_connection_fail_diplayed = false;

		if (LocalServiceTimer.isServiceStarted) {
			unbindService(maConnexionServiceNotification);
		}
		if (SilentService.isServiceStarted)
			unbindService(maConnexionServiceSilentMode);
		Log.d("Destroy", "on");
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	static AlarmServiceInterfaceImp _serviceInterface;
	public static ServiceConnection maConnexionServiceNotification = new ServiceConnection() {

		public void onServiceConnected(ComponentName name, IBinder service) {

			_serviceInterface = (AlarmServiceInterfaceImp) Stub
					.asInterface(service);

		}

		public void onServiceDisconnected(ComponentName name) {
			_serviceInterface = null;
		}
	};

	private SilentService monService;
	private ServiceConnection maConnexionServiceSilentMode = new ServiceConnection() {

		public void onServiceConnected(ComponentName className, IBinder service) {
			monService = ((SilentService.MonServiceSilentBinder) service)
					.getService();

		}

		public void onServiceDisconnected(ComponentName className) {
			monService = null;
		}
	};

	public static Intent IntentServiceNotification(Context context) {

		return new Intent(context, LocalServiceTimer.class);
	}

	public static Intent IntentServiceSilentMode(Context context) {

		return new Intent(context, SilentService.class);
	}

	public void InitialisationSettingTabTimerNotification(final Context context) {

		// spinner rayon

		String[] lischoises = getResources().getStringArray(
				R.array.rayon_nb_kilometre);
		ArrayAdapter<String> adapterRayon = new ArrayAdapter<String>(context,
				R.layout.my_spinner_style_arabe_adapter, lischoises);

		Spinner spinnerRayon = (Spinner) findViewById(R.id.RayonSetting);
		spinnerRayon.setAdapter(adapterRayon);
		spinnerRayon
				.setOnItemSelectedListener(new MyOnItemSelectedListenerRayonSetting());
		String rayonState = app_preferences.getString(Param.PREF_RAYON, "1");

		DataBaseQuery.rayon = Double.parseDouble(rayonState);
		if (rayonState.equals("5"))
			spinnerRayon.setSelection(0);
		else if (rayonState.equals("10"))
			spinnerRayon.setSelection(1);
		else if (rayonState.equals("100"))
			spinnerRayon.setSelection(2);
		else if (rayonState.equals("1000"))
			spinnerRayon.setSelection(3);
		// spinner Notification
		Spinner spinnerNotification = (Spinner) findViewById(R.id.NotificationSalatSetting);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.notification_setting_choises,
				android.R.layout.simple_spinner_dropdown_item);

		spinnerNotification.setAdapter(adapter);
		spinnerNotification
				.setOnItemSelectedListener(new MyOnItemSelectedListenerNotificationSetting());

		String notificationState = app_preferences.getString(
				Param.PREF_SERVICE_NOTIFICATION_STATE, Param.STATE_DISABLED);
		if (notificationState.equals(Param.STATE_DISABLED)) {
			spinnerNotification.setSelection(0);
			Utils.notificationChoises = Utils.NotificationChoises.DESACTIVER;
		} else if (notificationState.equals(Param.STATE_ENABLED)) {
			bindServiceNotification();
			LocalServiceTimer.isServiceStarted = true;
			spinnerNotification.setSelection(1);
			Utils.notificationChoises = Utils.NotificationChoises.ACTIVER;
		} else if (notificationState.equals(Param.STATE_ENABLED_SOUND)) {
			bindServiceNotification();
			LocalServiceTimer.isServiceStarted = true;
			spinnerNotification.setSelection(2);
			Utils.notificationChoises = Utils.NotificationChoises.ACTIVER_SON;
		} else if (notificationState.equals(Param.STATE_ENABLED_VIBRATION)) {
			bindServiceNotification();
			LocalServiceTimer.isServiceStarted = true;
			spinnerNotification.setSelection(3);
			Utils.notificationChoises = Utils.NotificationChoises.ACTIVER_VIBRATION;
		} else if (notificationState
				.equals(Param.STATE_ENABLED_SOUND_VIBRATION)) {
			bindServiceNotification();
			LocalServiceTimer.isServiceStarted = true;
			spinnerNotification.setSelection(4);
			Utils.notificationChoises = Utils.NotificationChoises.ACTIVER_SON_VIBRATION;
		}

		// spinnermethode de calcul
		Spinner spinnerMethCal = (Spinner) findViewById(R.id.MethCalculSalatSpinner);
		ArrayAdapter<CharSequence> adapterMethCal = ArrayAdapter
				.createFromResource(this, R.array.meth_cal_priere,
						android.R.layout.simple_spinner_dropdown_item);

		spinnerMethCal.setAdapter(adapterMethCal);
		spinnerMethCal
				.setOnItemSelectedListener(new MyOnItemSelectedListenerMethoCalcPriere());// !!!
		int methCalState = app_preferences.getInt(
				Param.PREF_PRAY_TIME_CAL_METHODE, 0);

		spinnerMethCal.setSelection(methCalState);

	}

	public void InitialisationSettingTabSilentMode(final Context context) {

		ToggleButton toggleButtonSilentMode = (ToggleButton) findViewById(R.id.auto_silent_activat_service);
		toggleButtonSilentMode
				.setOnClickListener(new MyOnClickListenerSilentMode());

		// config du service aut-silent-mode-pray-time
		Boolean silentSeviceState = app_preferences.getString(
				Param.PREF_SERVICE_SILENT_LAUNCHED, Param.STATE_DISABLED)
				.equals(Param.STATE_ENABLED);

		if (silentSeviceState) {
			SilentService.isServiceStarted = true;
			bindServiceSilentMode();

			((ToggleButton) findViewById(R.id.auto_silent_activat_service))
					.setChecked(true);
		} else
			((ToggleButton) findViewById(R.id.auto_silent_activat_service))
					.setChecked(false);
		int max, min;

		min = app_preferences.getInt(Param.PREF_FAJR_MIN,
				Param.DEFAULT_MIN_MINUTE_SILENT_MODE);
		max = app_preferences.getInt(Param.PREF_FAJR_MAX,
				Param.DEFAULT_MAX_MINUTE_SILENT_MODE);
		ViewGroup layout_fajr = (ViewGroup) findViewById(R.id.auto_silent_settings_layout_fajr_layout);
		layout_fajr
				.addView(implementationAutoSilentModeFor(PRAY.FAJR, min, max));

		min = app_preferences.getInt(Param.PREF_DHOHR_MIN,
				Param.DEFAULT_MIN_MINUTE_SILENT_MODE);
		max = app_preferences.getInt(Param.PREF_DHOHR_MAX,
				Param.DEFAULT_MAX_MINUTE_SILENT_MODE);
		ViewGroup layout_dhohr = (ViewGroup) findViewById(R.id.auto_silent_settings_layout_dhohr_layout);
		layout_dhohr.addView(implementationAutoSilentModeFor(PRAY.DHOHR, min,
				max));

		min = app_preferences.getInt(Param.PREF_ASR_MIN,
				Param.DEFAULT_MIN_MINUTE_SILENT_MODE);
		max = app_preferences.getInt(Param.PREF_ASR_MAX,
				Param.DEFAULT_MAX_MINUTE_SILENT_MODE);
		ViewGroup layout_asr = (ViewGroup) findViewById(R.id.auto_silent_settings_layout_asr_layout);
		layout_asr.addView(implementationAutoSilentModeFor(PRAY.ASR, min, max));

		min = app_preferences.getInt(Param.PREF_MAGHREB_MIN,
				Param.DEFAULT_MIN_MINUTE_SILENT_MODE);
		max = app_preferences.getInt(Param.PREF_MAGHREB_MAX,
				Param.DEFAULT_MAX_MINUTE_SILENT_MODE);
		ViewGroup layout_maghreb = (ViewGroup) findViewById(R.id.auto_silent_settings_layout_maghreb_layout);
		layout_maghreb.addView(implementationAutoSilentModeFor(PRAY.MAGHREB,
				min, max));

		min = app_preferences.getInt(Param.PREF_ISHA_MIN,
				Param.DEFAULT_MIN_MINUTE_SILENT_MODE);
		max = app_preferences.getInt(Param.PREF_ISHA_MAX,
				Param.DEFAULT_MAX_MINUTE_SILENT_MODE);
		ViewGroup layout_isha = (ViewGroup) findViewById(R.id.auto_silent_settings_layout_isha_layout);
		layout_isha
				.addView(implementationAutoSilentModeFor(PRAY.ISHA, min, max));

	}

	public RangeSeekBar<Integer> implementationAutoSilentModeFor(
			final PRAY pray, final int min, final int max) {
		RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(pray, min,
				max, this);
		seekBar
				.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {
					public void rangeSeekBarValuesChanged(Integer minValue,
							Integer maxValue) {

						SharedPreferences.Editor editor = app_preferences
								.edit();

						switch (pray) {

						case FAJR:
							editor.putInt(Param.PREF_FAJR_MIN, minValue);
							editor.putInt(Param.PREF_FAJR_MAX, maxValue);
							break;
						case DHOHR:
							editor.putInt(Param.PREF_DHOHR_MIN, minValue);
							editor.putInt(Param.PREF_DHOHR_MAX, maxValue);
							break;
						case ASR:
							editor.putInt(Param.PREF_ASR_MIN, minValue);
							editor.putInt(Param.PREF_ASR_MAX, maxValue);
							break;
						case MAGHREB:
							editor.putInt(Param.PREF_MAGHREB_MIN, minValue);
							editor.putInt(Param.PREF_MAGHREB_MAX, maxValue);
							break;
						case ISHA:
							editor.putInt(Param.PREF_ISHA_MIN, minValue);
							editor.putInt(Param.PREF_ISHA_MAX, maxValue);
							break;
						}
						editor.commit();

					}
				});
		return seekBar;

	}

	private void startServiceNotification() {

		startService(IntentServiceNotification(this));

	}

	private void stopServiceNotification() {

		stopService(IntentServiceNotification(this));
	}

	private void bindServiceNotification() {

		bindService(IntentServiceNotification(this),
				maConnexionServiceNotification, Context.BIND_AUTO_CREATE);

	}

	private void releaseServiceNotification() {

		unbindService(maConnexionServiceNotification);

	}

	private void startServiceSilentMode() {

		startService(IntentServiceSilentMode(this));

	}

	private void stopServiceSilentMode() {

		stopService(IntentServiceSilentMode(this));

	}

	private void bindServiceSilentMode() {

		bindService(IntentServiceSilentMode(this),
				maConnexionServiceSilentMode, Context.BIND_AUTO_CREATE);

	}

	private void releaseServiceSilentMode() {

		unbindService(maConnexionServiceSilentMode);

	}

	private class MyOnClickListenerSilentMode implements OnClickListener {

		public void onClick(View v) {
			final SharedPreferences.Editor editor = app_preferences.edit();
			if (((ToggleButton) v).isChecked()) {

				if (SilentService.isServiceStarted) {
					releaseServiceSilentMode();
					stopServiceSilentMode();
				}
				bindServiceSilentMode();
				startServiceSilentMode();
				SilentService.isServiceStarted = true;
				editor.putString(Param.PREF_SERVICE_SILENT_LAUNCHED,
						Param.STATE_ENABLED);

			}

			else if (SilentService.isServiceStarted) {
				releaseServiceSilentMode();
				stopServiceSilentMode();
				SilentService.isServiceStarted = false;
				editor.putString(Param.PREF_SERVICE_SILENT_LAUNCHED,
						Param.STATE_DISABLED);
			}
			editor.commit();
		}

	}

	private class MyOnItemSelectedListenerNotificationSetting implements
			OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			SharedPreferences.Editor editor = app_preferences.edit();

			switch (pos) {
			case 0:
				Utils.notificationChoises = Utils.NotificationChoises.DESACTIVER;
				editor.putString(Param.PREF_SERVICE_NOTIFICATION_STATE,
						Param.STATE_DISABLED);
				editor.commit();
				break;
			case 1:
				Utils.notificationChoises = Utils.NotificationChoises.ACTIVER;
				editor.putString(Param.PREF_SERVICE_NOTIFICATION_STATE,
						Param.STATE_ENABLED);
				editor.commit();
				break;
			case 2:
				Utils.notificationChoises = Utils.NotificationChoises.ACTIVER_SON;
				editor.putString(Param.PREF_SERVICE_NOTIFICATION_STATE,
						Param.STATE_ENABLED_SOUND);
				editor.commit();
				break;
			case 3:
				Utils.notificationChoises = Utils.NotificationChoises.ACTIVER_VIBRATION;
				editor.putString(Param.PREF_SERVICE_NOTIFICATION_STATE,
						Param.STATE_ENABLED_SOUND_VIBRATION);
				editor.commit();
				break;
			case 4:
				Utils.notificationChoises = Utils.NotificationChoises.ACTIVER_SON_VIBRATION;
				editor.putString(Param.PREF_SERVICE_NOTIFICATION_STATE,
						Param.STATE_ENABLED_SOUND_VIBRATION);
				editor.commit();
				break;
			}

			if (Utils.notificationChoises != Utils.NotificationChoises.DESACTIVER) {

				if (LocalServiceTimer.isServiceStarted) {
					releaseServiceNotification();
					stopServiceNotification();
				}

				bindServiceNotification();
				startServiceNotification();
				LocalServiceTimer.isServiceStarted = true;

			} else if (LocalServiceTimer.isServiceStarted) {
				releaseServiceNotification();
				stopServiceNotification();
				LocalServiceTimer.isServiceStarted = false;

			}
		}

		public void onNothingSelected(AdapterView<?> arg0) {

		}

	}

	public class MyOnItemSelectedListenerRayonSetting implements
			OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			SharedPreferences.Editor editor = app_preferences.edit();

			switch (pos) {
			case 0:

				editor.putString(Param.RAYON, "5");
				editor.commit();
				break;
			case 1:

				editor.putString(Param.RAYON, "10");
				editor.commit();
				break;
			case 2:

				editor.putString(Param.RAYON, "100");
				editor.commit();
				break;
			case 3:

				editor.putString(Param.RAYON, "1000");
				editor.commit();
				break;

			}

			DataBaseQuery.rayonChanged = true;
		}

		public void onNothingSelected(AdapterView<?> arg0) {
		}

	}

	public class MyOnItemSelectedListenerMethoCalcPriere implements
			OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			SharedPreferences.Editor editor = app_preferences.edit();

			editor.putInt(Param.PREF_PRAY_TIME_CAL_METHODE, pos);
			editor.commit();
			PrayTimeLayout.Renitialisation(MyApplicationMainActivity.mContext,
					pos);
		}

		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}

	Location lastKnownLocation;

	private void InitialisationCurrentLocation() {
		myLocation = null;

		LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		LocationListener mlocListener = new MyLocationListener();

		mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
				0, mlocListener);
		for (String provider : mlocManager.getAllProviders()) {
			Location loc = mlocManager.getLastKnownLocation(provider);
			if (loc != null) {
				myLocation = loc;
			}
		}

		if (myLocation != null) {
			mLatitude = myLocation.getLatitude();
			mLongitude = myLocation.getLongitude();
			myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
			mapController.animateTo(myGeoPoint);
			SharedPreferences.Editor editor = app_preferences.edit();
			editor.putString(Param.PREF_LATITUDE, mLatitude + "");
			editor.putString(Param.PREF_LONGITUDE, mLongitude + "");
			editor.commit();
		} else {

			String latitude_buff = app_preferences.getString(
					Param.PREF_LATITUDE, "");
			String longitude_buff = app_preferences.getString(
					Param.PREF_LONGITUDE, "");
			if (!latitude_buff.equals("") && !longitude_buff.equals("")) {
				mLatitude = Double.parseDouble(latitude_buff);
				mLongitude = Double.parseDouble(longitude_buff);
				myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
				mapController.animateTo(myGeoPoint);
			} else {

				AlertDialog.Builder builder = new AlertDialog.Builder(
						MyApplicationMainActivity.getmContext());
				builder.setMessage(
						MyApplicationMainActivity.getmContext().getString(
								R.string.dialog_need_to_connect_advice))
						.setCancelable(true).setNegativeButton(
								MyApplicationMainActivity.getmContext()
										.getString(R.string.dialog_ok),
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										dialog.cancel();
									}
								});
				AlertDialog alert = builder.create();
				alert.show();
				mLatitude = 48.855279;// 36.859502;//
				mLongitude = 2.287939;// 10.168097;//
				myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
				mapController.animateTo(myGeoPoint);
			}
		}

	}

	public class MyLocationListener implements LocationListener {

		public void onLocationChanged(Location loc)

		{
			if (loc != null) {
				// mLatitude = loc.getLatitude();
				// mLongitude = loc.getLongitude();
				myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
				mapController.animateTo(myGeoPoint);
				SharedPreferences.Editor editor = app_preferences.edit();
				editor.putString(Param.PREF_LATITUDE, mLatitude + "");
				editor.putString(Param.PREF_LONGITUDE, mLongitude + "");
				editor.commit();

			}

		}

		public void onProviderDisabled(String provider)

		{

			// Toast.makeText(getApplicationContext(), "Disabled",
			//
			// Toast.LENGTH_SHORT).show();

		}

		public void onProviderEnabled(String provider)

		{

		}

		public void onStatusChanged(String provider, int status, Bundle extras)

		{

		}

	}

}

//.setImageDrawable(getResources().getDrawable(R.drawable.maps_mode))

// Saisir les
// Toast.makeText(MyMapActivity.getmContext() , "Time :" ,
// Toast.LENGTH_LONG).show();

// retourner une ressource
// context.getString(R.string.loading)

/*
 * Set on full screan (au debut de onCreate)
 * this.requestWindowFeature(Window.FEATURE_NO_TITLE);
 * this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
 * WindowManager.LayoutParams.FLAG_FULLSCREEN );
 * super.onCreate(savedInstanceState);
 */